package com.xieyingwangluo.yxpart.logic;

import com.xieyingwangluo.yxpart.bean.Result;
import com.xieyingwangluo.yxpart.entity.UserInfoModel;
import com.xieyingwangluo.yxpart.entity.UserModel;
import com.xieyingwangluo.yxpart.service.UserInfoService;
import com.xieyingwangluo.yxpart.service.UserService;
import com.xieyingwangluo.yxpart.utils.StringBuildUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class UserLogic {
    @Autowired
    private UserService userService;
    @Autowired
    private UserInfoService userInfoService;

    //注册
    @Transactional
    public Result add(UserModel userModel) {
        Map<String, Object> map = new HashMap<>();
        userModel.setNoid(StringBuildUtils.buildUUID());
        //积分
        userModel.setIntg(600);
        //状态
        userModel.setState(0);
        //vip
        userModel.setVip(1);
        userModel.setHead_img("/src/main/resources/img/head.jpg");

        if (userModel.getTel() != null || !"".equals(userModel.getTel())) {
            UserModel user = new UserModel();
            user.setTel(userModel.getTel());
            List<UserModel> list = userService.selT(user.getTel());
            if (list.size() > 0) {
                return Result.generate(1, "User already exists", null);
            }
        }
        int cnt = userService.add(userModel);
        if (cnt > 0) {
            UserInfoModel userInfoModel = new UserInfoModel();
            userInfoModel.setNoid(StringBuildUtils.buildUUID());
            userInfoModel.setName(userModel.getName());
            userInfoModel.setSex(0);//默认0男
            userInfoModel.setBirth("19710101");
            userInfoModel.setIdnumb("130000000000000000");//身份证号
            userInfoModel.setCounty("130000");
            userInfoModel.setAddress("石家庄市桥西区");
            userInfoModel.setHeight(160);
            userInfoModel.setWeight(50);
            userInfoModel.setQq("0000000000");
            userInfoModel.setEmail("0000000000@qq.com");
            userInfoModel.setInfo("超厉害的！");
            userInfoModel.setUser_id(userModel.getNoid());
            userInfoModel.setState(1);//未审核
            userInfoService.add(userInfoModel);
            map.put("userModel", userInfoModel.getNoid());
            map.put("userInfoModel", userInfoModel);
            return Result.generate(0, "Add success", map);
        }
        return Result.generate(1, "Add fail", null);
    }


    //登录
    public Result checklogin(HttpSession session,
                             String tel,
                             String pwd) {
        Map<String, Object> map = new HashMap<>();
        UserModel userModel = userService.checklogin(tel, pwd);
        if (userModel != null ) {
            session.setAttribute("user", userModel);
            map.put("User", userModel);
            map.put("Login", "普通用户");
            return Result.generate(0, "Login success", map);
        }
        return Result.generate(1, "Login fail", null);
    }

    //普通用户退出登录
    public Result outlogin(HttpSession session) {
        session.getAttribute("user");
        session.invalidate();
        return Result.generate(0, "Out success", null);
    }

    //更新普通用户密码
    public Result updPwd(String noid, String pwd, String oldpwd) {
        UserModel user = userService.getById(noid);

        if (oldpwd.equals(user.getPwd())) {
            int cnt = userService.updPwd(noid, pwd);
            return Result.generate(0, "Upd success", cnt);
        }
        return Result.generate(1, "Upd fail", null);

    }

    //更新用户
    public Result update(UserModel userModel) {
        UserModel user = userService.getById(userModel.getNoid());
        if (userModel.getTel() != null) {
            if (user.getTel().equals(userModel.getTel())) {
                int cnt = userService.update(userModel);
                return Result.generate(0, "Upd success", cnt);
            } else {
                List<UserModel> list = userService.selT(userModel.getTel());
                if (list.size() == 0) {
                    int cnt = userService.update(userModel);
                    return Result.generate(0, "Upd success", cnt);
                }
            }
        }
        return Result.generate(1, "Upd fail", null);
    }

    //查询用户信息
    public Result getById(String noid) {
        UserModel userModel = userService.getById(noid);
        if (userModel != null) {
            return Result.generate(0, "Select success", userModel);
        }
        return Result.generate(1, "Select fail", null);
    }

    //模糊查询所有用户
    public Result selA(UserModel userModel) {
        if (userModel != null) {
            List<UserModel> userModelList = userService.selA(userModel);
            return Result.generate(0, "Select success", userModelList);
        }
        return Result.generate(1, "Select fail", null);
    }
}
